package com.api.shopapi.mapper;

import com.api.shopapi.entity.MedicationGroup;
import com.api.shopapi.entity.dto.MedicationGroupDTO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface MedicationGroupMapper {

    @Insert("insert into medication_group " +
            "(MedicationGroupCode,Name,Status,StaffCode,InstitutionCode,ElderlyCode) " +
            "values (#{medicationGroup.MedicationGroupCode} ,#{medicationGroup.Name} ," +
            "#{medicationGroup.Status} ,#{medicationGroup.StaffCode},#{medicationGroup.InstitutionCode},#{medicationGroup.ElderlyCode} )")
    int insert(@Param("medicationGroup") MedicationGroup medicationGroup);

    @Update("update medication_group set Name=#{medicationGroup.Name},Status=#{medicationGroup.Status} ," +
            "StaffCode=#{medicationGroup.StaffCode},InstitutionCode=#{medicationGroup.InstitutionCode},ElderlyCode=#{medicationGroup.ElderlyCode} " +
            "where MedicationGroupCode=#{medicationGroup.MedicationGroupCode} ")
    int updateByMedicationGroupCode(@Param("medicationGroup") MedicationGroup medicationGroup);

    @Select("select * from medication_group where ElderlyCode=#{elderlyCode} limit 1")
    MedicationGroup getByElderlyCode(@Param("elderlyCode") String elderlyCode);

    @Select("select * from medication_group where MedicationGroupCode=#{medicationGroupCode} limit 1")
    MedicationGroup getByMedicationGroupCode(@Param("medicationGroupCode") String medicationGroupCode);

    @Select("<script>" +
            "select m.*,u.Name ElderlyName from medication_group m,elderly e,user u " +
            "where m.InstitutionCode=#{institutionCode} and m.elderlyCode=e.elderlyCode and e.userCode = u.userCode " +
            "<if test ='elderlyName !=null and elderlyName!=\"\" '>" +
            "and u.Name like concat('%',#{elderlyName},'%') " +
            "</if>" +
            "<if test ='status !=-1 '>" +
            "and m.Status =#{status} " +
            "</if>" +
            "</script>")
    List<MedicationGroupDTO> getPageList(@Param("institutionCode") String institutionCode, @Param("status") int status, @Param("elderlyName") String elderlyName);

    @Select("<script>" +
            "select * from medication_group " +
            "where InstitutionCode in (select institutionCode from institution where governmentCode=#{governmentCode} )" +

            "</script>")
    List<MedicationGroup> listByGovCode(@Param("governmentCode") String governmentCode);


}
